package com.jwinf.cdLibary.controller;

import com.jwinf.cdLibary.domain.User;
import com.jwinf.cdLibary.service.UserService;
import com.jwinf.cdLibary.tool.VerifyText;
import com.jwinf.cdLibary.tool.WindowUtils;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author jwinf
 * 抽象类 抽象出了添加和编辑用户的共有操作
 */
public abstract class OperateUserFrameController implements Initializable {
    @FXML
    protected Label userIcon;
    @FXML
    protected Label pwdIcon;
    @FXML
    protected Label repIcon;
    @FXML
    protected Label telIcon;
    @FXML
    protected Label addIcon;
    @FXML
    protected TextField usertxt;
    @FXML
    protected TextField pwdtxt;
    @FXML
    protected TextField reptxt;
    @FXML
    protected TextField teltxt;
    @FXML
    protected TextField addtxt;
    @FXML
    protected Label wrongpwdlbl;
    @FXML
    protected Label wrongreplbl;
    @FXML
    protected Label wrongtellbl;
    @FXML
    protected Label tippwd;
    @FXML
    protected Label tiprep;
    @FXML
    protected Label tiptel;
    @FXML
    protected Button submitbtn;
    @FXML
    protected Button cancelbtn;

    protected boolean pwdpass = false;
    protected boolean reppass = false;
    protected boolean telpass = false;

    protected UserService userService = new UserService();
    protected User user = new User();

    // 初始化方法
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        componentInit();           // 组件初始化
        addListener();             // 添加监听器
    }


    // 组件初始化
    private void componentInit(){
        // 设置提示标签不可见
        wrongpwdlbl.setVisible(false);
        wrongreplbl.setVisible(false);
        wrongtellbl.setVisible(false);
        tiptel.setVisible(false);
        tippwd.setVisible(false);
        tiprep.setVisible(false);

        // 给标签设置icon样式
        Image image;
        image = new Image(getClass().getResourceAsStream("/icon/newUser.png"), 30, 30, false, false);
        userIcon.setGraphic(new ImageView(image));

        image = new Image(getClass().getResourceAsStream("/icon/newPwd.png"), 30, 30, false, false);
        pwdIcon.setGraphic(new ImageView(image));
        repIcon.setGraphic(new ImageView(image));

        image = new Image(getClass().getResourceAsStream("/icon/newTel.png"), 30, 30, false, false);
        telIcon.setGraphic(new ImageView(image));

        image = new Image(getClass().getResourceAsStream("/icon/newAdd.png"), 30, 30, false, false);
        addIcon.setGraphic(new ImageView(image));

    }

    // 添加监听器
    protected void addListener(){
        // 鼠标进入组件时 改变鼠标样式
        WindowUtils.changeMouse(cancelbtn);
        WindowUtils.changeMouse(submitbtn);
        // 执行耗时操作时 改变鼠标样式
        WindowUtils.MouseWait(submitbtn);

        // 添加文本监听方法 根据输入的内容提示用户
        documentHandel(pwdtxt, wrongpwdlbl, tippwd, "pwd");
        documentHandel(reptxt, wrongreplbl, tiprep, "rep");
        documentHandel(teltxt, wrongtellbl, tiptel, "tel");
    }

    // 返回用户管理界面
    @FXML
    protected void cancel() throws IOException {
        WindowUtils.showFrame("/com/jwinf/cdLibary/view/UserManageFrame.fxml", "/icon/loginIcon.png", "管理员主界面");
        WindowUtils.destroyStage(cancelbtn);
    }

    // 提交方法 具体操作交由子类实现
    @FXML
    abstract protected void submit();

    // 文档监听方法 根据flag来标识要进行的操作
    private void documentHandel(TextField textField, Label text, Label icon, String flag){
        switch (flag){
            case "pwd":
                textField.textProperty().addListener(((observable, oldValue, newValue) -> {
                    if(VerifyText.veryfyPwd(textField.getText())){
                        text.setVisible(false);
                        icon.setGraphic(new ImageView(new Image(getClass().getResourceAsStream("/icon/tipRight.png"), 30, 30, false, false )));
                        pwdpass = true;
                    }else {
                        text.setVisible(true);
                        icon.setGraphic(new ImageView(new Image(getClass().getResourceAsStream("/icon/tipWrong.png"), 30, 30, false, false)));
                        pwdpass = false;
                    }
                    icon.setVisible(true);
                }));
                break;
            case "tel":
                textField.textProperty().addListener(((observable, oldValue, newValue) -> {
                    if(VerifyText.veryfyTel(textField.getText())){
                        text.setVisible(false);
                        icon.setGraphic(new ImageView(new Image(getClass().getResourceAsStream("/icon/tipRight.png"), 30, 30, false, false )));
                        telpass = true;
                    }else {
                        text.setVisible(true);
                        icon.setGraphic(new ImageView(new Image(getClass().getResourceAsStream("/icon/tipWrong.png"), 30, 30, false, false)));
                        telpass = false;
                    }
                    icon.setVisible(true);
                }));
                break;
            case "rep":
                textField.textProperty().addListener(((observable, oldValue, newValue) -> {
                    if((pwdtxt.getText().equals(textField.getText()))){
                        text.setVisible(false);
                        icon.setGraphic(new ImageView(new Image(getClass().getResourceAsStream("/icon/tipRight.png"), 30, 30, false, false )));
                        reppass = true;
                    }else {
                        text.setVisible(true);
                        icon.setGraphic(new ImageView(new Image(getClass().getResourceAsStream("/icon/tipWrong.png"), 30, 30, false, false)));
                        reppass = false;
                    }
                    icon.setVisible(true);
                }));

        }

    }

    // 检验数据合法性方法
    protected boolean check(String str, String message){
        if(str == null || str.isEmpty()){
            WindowUtils.showTip(message, "/icon/warnIcon.png", "-fx-background-color:  linear-gradient(to top, #f3e7e9 0%, #e3eeff 99%, #e3eeff 100%);");
            return false;
        }
        return true;
    }
}
